<script>
export default {
  data() {
    return {
      timer: null,
      animate: false,
      newsList: [
        {
          id: 1,
          title: '新闻标题1',
          description: '新闻描述1'
        },
        {
          id: 2,
          title: '新闻标题2',
          description: '新闻描述2'
        },
        {
          id: 3,
          title: '新闻标题3',
          description: '新闻描述3'
        },
        {
          id: 4,
          title: '新闻标题4',
          description: '新闻描述4'
        },
        {
          id: 5,
          title: '新闻标题5',
          description: '新闻描述5'
        }
      ]
    }
  },
  mounted() {
    this.scrollUp()
  },
  beforeUnmount() {
    this.stop()
  },
  methods: {
    scrollUp() {
      this.timer = setInterval(() => {
        this.animate = true
        setTimeout(() => {
          this.newsList.push(this.newsList[0])
          this.newsList.shift()
          this.animate = false
        }, 500)
      }, 5000)
    },
    stop() {
      clearInterval(this.timer)
    },
    up() {
      this.scrollUp()
    },
    handleDetail(item) {
      console.log(item)
    },
    handleDelete(item, index) {
      this.newsList.splice(index, 1)
    }
  }
}
</script>

<template>
  <div
    :class="{ anim: animate }"
    @mouseenter="stop()"
    @mouseleave="up()"
    class="unreadMsg"
  >
    <div
      class="news_name"
      v-for="(item, index) in newsList"
      :key="item.id"
      @click="handleDetail(item)"
    >
      <div class="content">
        <div class="title">{{ item.title }}</div>
        <div class="des">{{ item.description }}</div>
      </div>
      <span @click="handleDelete(item, index)" class="close">
        <el-icon style="cursor: pointer">
          <Close />
        </el-icon>
      </span>
    </div>
  </div>
</template>

<style lang="less" scoped>
.unreadMsg {
  max-height: 1000px;
  overflow: hidden;
  position: absolute;
  right: 0;
  top: 0;
  bottom: auto;
  margin-inline-end: 24px;

  .news_name {
    line-height: 30px;
    // transition: top 0.5s;
    // transition-delay: 10s;
    position: relative;

    .content {
      position: relative;
      width: 384px;
      margin-bottom: 40px;
      margin-inline-start: auto;
      padding: 20px 30px;
      overflow: hidden;
      word-wrap: break-word;
      background: #fff;
      border-radius: 8px;

      .title {
        padding-right: 12px;
        margin-bottom: 8px;
        color: rgba(0, 0, 0, 0.88);
        font-size: 16px;
        line-height: 1.5;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      .des {
        font-size: 14px;
        cursor: pointer;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
      }
    }

    .close {
      position: absolute;
      top: 20px;
      inset-inline-end: 24px;
      color: rgba(0, 0, 0, 0.45);
      outline: none;
      width: 22px;
      height: 22px;
      border-radius: 4px;
      transition:
        background-color 0.2s,
        color 0.2s;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

.anim {
  transition: all 0.5s;
  margin-top: -110px;
}
</style>
